package com.schoolegard.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.schoolegard.services.RegionsService;
import com.schoolgard.dao.RegionsDao;
import com.schoolgard.entitybean.Regions;

public class RegionsServiceImpl implements RegionsService {
	
	private static Logger logger = Logger.getLogger(RegionsServiceImpl.class);
	
	private RegionsDao regionsDao;

	public void setRegionsDao(RegionsDao regionsDao) {
		this.regionsDao = regionsDao;
	}

	public Long saveOrUpdate(Regions regions) {
		try{
		Regions entity = regionsDao.createOrModify(regions);
		return entity.getId();
		}catch(Exception e) {
			logger.error("保存菜单表失败！",e);
			return 0L;
		}
	}

	public void delete(Long id) {
		regionsDao.remove(findById(id));
	}
	
	public int delete(Regions regions){
		try{
			regionsDao.remove(regions);
		}catch(Exception e){
			logger.error("删除失败！");
			return 0;
		}
		return 1;
	}

	public List<Regions> findLevelOne() {
		String hql = "from Regions where parentId = 0 and id <> -1 order by id asc";
		List<Regions> regionsList = regionsDao.find(hql);
		return regionsList == null ? new ArrayList<Regions>(0) : regionsList;
	}

	public List<Regions> findListByParentId(Long id) {
		String hql = "from Regions where parentId = ? order by id asc";
		List<Regions> regionsList = regionsDao.find(hql, id);
		return regionsList == null ? new ArrayList<Regions>(0) : regionsList;
	}
	
	public Regions findById(Long id){
		return regionsDao.findById(id);
	}

	@Override
	public List<Regions> findAll() {
		return regionsDao.findAll();
	}
}
